//
//  NewsDetailViewController.m
//  Conference
//
//  Created by 陈鑫 on 12-11-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "NewsDetailViewController.h"
#import "NewsData.h"
#import "Globals.h"
#import "AsyncImageView.h"
#import "NetContant.h"
#import "ShowImageController.h"
@interface NewsDetailViewController ()

@end

@implementation NewsDetailViewController
@synthesize lab_coming;
@synthesize lab_title;
@synthesize lab_time;
@synthesize lab_content;
@synthesize newsData;
@synthesize scrollView;
@synthesize Height;
@synthesize arrayOfUrl;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.lab_title.text=self.newsData.title;
    self.lab_coming.text=[NSString stringWithFormat:@"来源：%@",self.newsData.writer];
    self.lab_time.text=self.newsData.dateTime;
    
    
    NSString *content=[NSString stringWithFormat:@"%@",self.newsData.message];
    
    UIFont *font = [UIFont systemFontOfSize:16];
    
    CGSize size=[[Globals getInstance]CalculationCellSize:self.scrollView.frame.size.width fontSize:16 content:content];
    
    CGRect rect = [self.lab_content textRectForBounds:self.lab_content.frame limitedToNumberOfLines:0];
    rect.size=size;
    self.lab_content.frame = rect;
    self.lab_content.text=content;
    self.lab_content.numberOfLines = 0;
    self.lab_content.font = font;
    
   self.Height=size.height+20;
    
    [self.scrollView setContentSize:CGSizeMake(260,self.Height)];
    //
   
    NSString *imgUri=self.newsData.arrImg;
    
    
    if(nil!=imgUri||![imgUri isEqualToString:@""]){
        NSArray *img=[imgUri componentsSeparatedByString:[NetContant getInstance].IMGURL_SPLIT_FLAG];
        
        self.arrayOfUrl=[[Globals getInstance]addDefaultImage:img scroll:self.scrollView height:self.Height];
        
        NSThread *thread=[[[NSThread alloc] initWithTarget:self selector:@selector(run:) object:img] autorelease];
        [thread start];

    }
    // Do any additional setup after loading the view from its nib.
}
-(void)run:(NSArray*)img{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSMutableArray *arrayOfImage=[[Globals getInstance]getImageArray:img];
    [self performSelectorOnMainThread:@selector(onPostExecute:) withObject:arrayOfImage waitUntilDone:YES];
    
	[pool release];
    
}
-(void)onPostExecute:(NSMutableArray *)arrayOfImage{
    NSArray *views = [self.scrollView subviews];
    for(id view in views)
    {
        if([view isKindOfClass:[UIImageView class]])
            [view removeFromSuperview];
    }
    
    for(int i=0;i<[arrayOfImage count];i++){
        UIImageView *imageView=[arrayOfImage objectAtIndex:i];
        imageView.frame=CGRectMake(0, self.Height, imageView.frame.size.width, imageView.frame.size.height);
        imageView.center=CGPointMake(140, imageView.center.y);
        [self.scrollView addSubview:imageView];
        self.Height=self.Height+imageView.frame.size.height+20;
        
        //点击
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(UesrClicked:)];
        [imageView addGestureRecognizer:singleTap];
        [singleTap release];
        
        [self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width, self.Height)];
    }
    
}

-(void)UesrClicked:(UIGestureRecognizer *)gestureRecognizer{
    UIImageView *imageView = (UIImageView *)[gestureRecognizer view];
    int index = imageView.tag;
    
    ShowImageController *imgController=[[ShowImageController alloc]initWithNibName:@"ShowImageController" bundle:nil];
    imgController.url=[self.arrayOfUrl objectAtIndex:index];
    //    [self presentModalViewController:imgController animated:YES];
    
    [self.navigationController pushViewController:imgController animated:YES];
    [imgController release];
}
- (void)viewDidUnload
{
    [self setArrayOfUrl:nil];
    [self setNewsData:nil];
    [self setLab_title:nil];
    [self setLab_time:nil];
    [self setLab_content:nil];
    [self setScrollView:nil];
    [self setLab_coming:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [arrayOfUrl release];
    [newsData release];
    [lab_title release];
    [lab_time release];
    [lab_content release];
    [scrollView release];
    [lab_coming release];
    [super dealloc];
}
- (IBAction)btnBackAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
